<script>
    $(function(){

        var form = "#<?=$args->form?>-form";

        $(form).ajaxForm({
            url: 'ajax/forms.show.<?=$args->form?>',
            data:{
                ajax_form: 1,
                submit_<?=$form->id?>: 'submit'
            },
            dataType: 'json',
            beforeSubmit: function(){
                $(form+" input").removeClass('error');
            },
            success: function(data){
                if(data.result == 'error'){

                    $.each(data.errors,function(index, value){
                        $(form+" [name="+index+"]").addClass('error');
                    });
                }else{
                    $(form).trigger('reset');

                    $(form).hide().closest('div').append('<div class="success_message formDiv"><h3>'+data.message+'</h3></div>');

                    setTimeout(function(){
                        $(form).show();
                        $(form).closest('div').find('.success_message').remove()
;                    },2000);
                }
            }
        });

    });
</script>
<div style="display:none">
    <div id="<?=$args->form?>" class="popup">
        <h2><?=$form->title_form?></h2>
        <form id="<?=$args->form?>-form" action="" method="post">
            <?foreach($field_rows as $row) { ?>
                <?if($row['type_id'] == 'text') { ?>
                    <div class="formDiv">
                        <label for="<?=$form->id?>-<?=$row['nameid']?>"><?=$row['name']?><?=$row['valid_empty']? '<span>*</span>':''?></label>
                        <input type="text" id="<?=$form->id?>-<?=$row['nameid']?>" name="<?=$row['nameid']?>" <?=$row['valid_empty']?'required=""':''?>>
                    </div>
                <?}elseif($row['type_id'] == 'memo'){?>

                <?}elseif($row['type_id'] == 'select'){?>
                    <div class="formDiv"><label><?=$row['name']?><?=$row['valid_empty']? '<span>*</span>':''?></label>
                        <select name="<?=$row['nameid']?>">
                            <?
                            $options = explode("\n", $row['select_options']);
                            foreach($options as $option) {
                            ?>
                            <option value="<?=htmlspecialchars($option)?>"><?=htmlspecialchars($option)?></option>
                            <?}?>
                        </select>
                    </div>
                <?}?>
            <?}?>

            <button class="sub-button" type="submit"><?=$form->btn_name?$form->btn_name:'Отправить'?></button>
        </form>
    </div>
</div>